package com.artisan.web.action;

import com.artisan.common.pojo.PageBean;
import com.artisan.medium.admin.service.CustomerService;
import com.artisan.medium.admin.service.UserService;
import com.artisan.medium.admin.service.WorkerService;
import com.artisan.model.AddressInfos;
import com.artisan.model.Customer;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/7/2.
 */
@Controller
@RequestMapping("/admin/cust/")
public class CustomerController {

	private static final Logger log = LogManager.getLogger(CustomerController.class);

	@Autowired
	private CustomerService customerService;

	@Autowired
	private WorkerService workerService;

	/**
	 * 用户列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("customer_list_page")
	public ModelAndView customer_list_page(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Integer> result = new HashMap<>();
		return new ModelAndView("customer/customer_list", result);
	}

	/**
	 * 用户意见反馈查看页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("customer_suggest_page")
	public ModelAndView customer_suggest_page(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Integer> result = new HashMap<>();
		return new ModelAndView("customer/customer_suggest", result);
	}

	/**
	 * 查询用户信息
	 * @param request
	 * @param response
	 * @param phone
	 * @param proId
	 * @param cityId
	 * @param areaId
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryCustomers")
	public Object queryCustomer(HttpServletRequest request, HttpServletResponse response, String phone, String proId, String cityId,
	                            String areaId, @RequestParam(defaultValue = "0") Integer offset, @RequestParam(defaultValue = "10") Integer limit) {

		//List<AddressInfos> provinces = workerService.qProvinces();

		PageBean pageBean = customerService.queryCustomer(phone, proId, cityId, areaId, offset/limit + 1, limit);
		return pageBean;
	}

	/**
	 *
	 * @param request
	 * @param response
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryCustomerSuggest")
	public Object queryCustomerSuggest(HttpServletRequest request, HttpServletResponse response,
	                                   @RequestParam(defaultValue = "0") Integer offset, @RequestParam(defaultValue = "10") Integer limit) {

		PageBean pageBean = customerService.queryCustomerSuggestBack(offset/limit + 1, limit);
		return pageBean;
	}

}
